package starter;

import java.util.Properties;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import euclid.*;

/**
 * Main class for the starter
 *
 */
public class Starter {
	private static ORB orb;
	private static Coordinator coordinator;

	/**
	 * main class of the starter
	 * @param args
	 */
	public static void main(String[] args) {
		//args[0] = host of orbd
		//args[1] = port of orbd
		//args[2] = name of the coordinator to bind
		//args[3] = name of the starter
		if(args.length != 4)
		{
			System.err.println("<orbd host> <orbd port> <coordinator> <name of the starter>");
			System.exit(-1);
		}
				try {
					Integer.parseInt(args[1]);//check if the port is an integer
					String coordName = args[2];
					String starterName = args[3];
					Properties props = new Properties();
					props.put("org.omg.CORBA.ORBInitialPort", args[1]);
					props.put("org.omg.CORBA.ORBInitialHost",args[0]);
					orb = ORB.init(args,props);
					System.out.println("ORB initialized.");
					
					//get the reference of rootPOA and activate POA Manager
					POA rootPoa =
							POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
					rootPoa.the_POAManager().activate();
					
					NamingContextExt nc = NamingContextExtHelper.narrow(
							orb.resolve_initial_references("NameService"));
					
					//get the reference of the coordinator with the name given by the user
	                coordinator = CoordinatorHelper.narrow(nc.resolve_str(coordName));
	                
	              //Creation of the servant
					StarterImpl starter = new StarterImpl(starterName,rootPoa, coordinator);
					rootPoa.activate_object(starter);
					
					//get the object reference of starter
					org.omg.CORBA.Object ref = rootPoa.servant_to_reference(starter);
					
					euclid.Starter starterRef = StarterHelper.narrow(ref);
							
					// get the root naming context
			        org.omg.CORBA.Object objRef =
			        orb.resolve_initial_references("NameService");
			        //Use NamingContextExt instead of NamingContext. This is
			        // part of the Interoperable naming Service.
			        NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			        
			      //bind the reference of "starterRef" with the name chosen by the user (args[2])
					ncRef.rebind(ncRef.to_name(starterName), starterRef);
	                
	                //log the starter to the coordinator
	                coordinator.logStarter(starterRef);
				           
					System.out.println("Starter " + starterName + " is ready.");
					
					orb.run();
							
							
				}catch(NumberFormatException e){
					System.err.println("Please check the port");
				}catch(Exception e){
						System.err.println("Problem with the connexion. Please check the parameters.");
				}

	}

}
